﻿using JT808.DotNetty.Core;
using JT808.DotNetty.Core.Handlers;
using JT808.DotNetty.Core.Interfaces;
using JT808.DotNetty.Core.Metadata;
using JT808.Protocol;
using JT808.Protocol.MessageBody;
using CMS.Service;
using CMS.IService;
using CMS.DTO;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;

namespace JT808.DotNetty.SimpleServer.Handlers
{
    public class JT808MsgIdTcpSimpleHandler : JT808MsgIdTcpHandlerBase
    {
        private JT808Serializer jT808Serializer;
        private ICollectMachineGpsService collectMachineGpsService;
        public JT808MsgIdTcpSimpleHandler(
            ICollectMachineGpsService collectMachineGpsService,
            JT808Serializer jT808Serializer,
            ILoggerFactory loggerFactory,
            JT808TcpSessionManager sessionManager) : base(sessionManager)
        {
            this.collectMachineGpsService = collectMachineGpsService;
            this.jT808Serializer = jT808Serializer;
            logger = loggerFactory.CreateLogger<JT808MsgIdTcpSimpleHandler>();
        }

        private readonly ILogger<JT808MsgIdTcpSimpleHandler> logger;

        public override IJT808Reply Msg0x0200(JT808Request request)
        {
            JT808_0x0200 t = jT808Serializer.Deserialize<JT808_0x0200>(request.Package.Bodies);
            collectMachineGpsService.Save(new CollectMachineGpsDTO { id=Guid.NewGuid(), latitude= t.Lat, longitude=t.Lng, createtime=t.GPSTime,
                altitude =t.Altitude, speed=t.Speed, machineId=request.Package.Header.TerminalPhoneNo }, null);
            logger.LogDebug("Tcp_Msg0x0200");
            return base.Msg0x0200(request);
        }

        public override IJT808Reply Msg0x0001(JT808Request request)
        {
            logger.LogDebug("Tcp_Msg0x0001");
            return base.Msg0x0001(request);
        }

        public override IJT808Reply Msg0x0002(JT808Request request)
        {
            logger.LogDebug("Tcp_Msg0x0002");
            return base.Msg0x0002(request);
        }

        public override IJT808Reply Msg0x0003(JT808Request request)
        {
            logger.LogDebug("Tcp_Msg0x0003");
            return base.Msg0x0003(request);
        }

        public override IJT808Reply Msg0x0100(JT808Request request)
        {
            logger.LogDebug("Tcp_Msg0x0100");
            return base.Msg0x0100(request);
        }

        public override IJT808Reply Msg0x0102(JT808Request request)
        {
            logger.LogDebug("Tcp_Msg0x0102");
            return base.Msg0x0102(request);
        }
    }
}
